第十一页:函数

函数是编程需学的最后一个基本组成。所有的程序语言都是
函数。函数是一些角次可调用的、无须重写的东西。

如果你想教会自己快速阅读并且用一个一旦点击可告诉你当前
时间的长文本链接。

例如…时间!

看源码:


<a href="#" onClick="

var the_date = new Date();

var the_hour = the_date.getHours();

var the_minute = the_date.getMinutes();

var the_second = the_date.getSeconds();

var the_time = the_hour + ':' + the_minute + ':' + the_second;

alert('The time is now: ' + the_time);">time!</a>

在这里这段JavaScript的工作细节并不重要;一会我们再回来
复习一下。

重要的是它太长了。若这些时间链接再有10个,你须每次剪贴
这段程序。这使你的HTML既长且难看。另外,若你想改变这段
程序,就必须在10个不同地方改变。

你可以写一个函数来执行而不用作10次拷贝程序。这里的函数
使用变的即容易编辑又容易阅读。

请看如何写一段计时函数